package top.drinyeit.nongmao.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.adapter.SecondCategoryAdapter;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.model.CategoryBean;
import top.drinyeit.nongmao.presenter.SecondCategoryPresenter;

/**
 * Created by wu on 2018/1/25.
 */

public class SecondCategoryActivity extends BaseActivity {
    public CategoryBean category;
    private TextView price, sold;
    private RelativeLayout price_sort, sold_sort;
    public RecyclerView secondCategoryRecyclerView, mRecyclerView;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;
    private SecondCategoryPresenter secondCategoryPresenter;
    private int category_id;
    private SecondCategoryAdapter secondCategoryAdapter;
    private String categoryName;
    public int type = 1;
    private int priceNo = 0;
    private int soldNo = 0;
    public int pageIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_category);

        initView();
        initIntent();
        initData();
        initListener();
    }


    public void initView() {
        price = findViewById(R.id.price);
        sold = findViewById(R.id.sold);
        price_sort = findViewById(R.id.price_sort);
        sold_sort = findViewById(R.id.sold_sort);
        secondCategoryRecyclerView = findViewById(R.id.rv_second_category);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        shSwipeRefreshLayout = findViewById(R.id.mShswipeRefreshLayout);

    }


    public void initIntent() {
        category = (CategoryBean) getIntent().getExtras().get("category");
        category_id = category.getId();
        categoryName = category.getCategory_name();

        setToolbar2(true, categoryName);

        initRecycler();
    }

    private void initRecycler() {
        secondCategoryAdapter = new SecondCategoryAdapter(category.getSec_categorys());
        secondCategoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        secondCategoryRecyclerView.setAdapter(secondCategoryAdapter);
        secondCategoryAdapter.setOnItemClickListener(new SecondCategoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (position == 0) {
                    type = 1;
                    category_id = category.getId();
                } else {
                    type = 2;
                    category_id = category.getSec_categorys().get(position - 1).getId();
                }
                getCategory();
            }
        });
    }

    public void initData() {
        secondCategoryPresenter = new SecondCategoryPresenter(this);
        getCategory();
    }


    public void initListener() {
        shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                Map<String, Object> map = new HashMap<>();
                map.put("type", type);
                map.put("action", "commodity_category_goods");
                map.put("category_id", category_id);
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", 1);
                secondCategoryPresenter.categorySearch(map);
            }

            @Override
            public void onLoading() {
                Map<String, Object> map = new HashMap<>();
                map.put("type", type);
                map.put("action", "commodity_category_goods");
                map.put("category_id", category_id);
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", pageIndex + 1);
                secondCategoryPresenter.categorySearch(map);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
        price_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                priceSort();
            }
        });
        sold_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soldSort();
            }
        });
    }

    private void priceSort() {
        sold.setTextColor(ContextCompat.getColor(this, R.color.text_black_51));
        sold.setCompoundDrawables(null, null, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_goods");
        map.put("category_id", category_id);
        Drawable drawable0 = ContextCompat.getDrawable(this, R.drawable.up_arrow_icon);
        Drawable drawable1 = ContextCompat.getDrawable(this, R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (priceNo == 0) {
            map.put("price_type", 1);
            map.put("page_index", 1);
            map.put("type", type);
            secondCategoryPresenter.categorySearch(map);
            price.setTextColor(ContextCompat.getColor(this, R.color.app_green));
            price.setCompoundDrawables(null, null, drawable0, null);
            priceNo = 1;
            soldNo = 0;
        } else if (priceNo == 1) {
            map.put("price_type", 2);
            map.put("page_index", 1);
            map.put("type", type);
            secondCategoryPresenter.categorySearch(map);
            price.setCompoundDrawables(null, null, drawable1, null);
            priceNo = 2;
        } else {
            map.put("price_type", 1);
            map.put("page_index", 1);
            map.put("type", type);
            secondCategoryPresenter.categorySearch(map);
            price.setCompoundDrawables(null, null, drawable0, null);
            priceNo = 1;
        }
    }

    private void soldSort() {
        price.setTextColor(ContextCompat.getColor(this, R.color.text_black_51));
        price.setCompoundDrawables(null, null, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_goods");
        map.put("category_id", category_id);
        Drawable drawable0 = ContextCompat.getDrawable(this, R.drawable.up_arrow_icon);
        Drawable drawable1 = ContextCompat.getDrawable(this, R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (soldNo == 0) {
            map.put("sell_type", 1);
            map.put("page_index", 1);
            map.put("type", type);
            secondCategoryPresenter.categorySearch(map);
            sold.setTextColor(ContextCompat.getColor(this, R.color.app_green));
            sold.setCompoundDrawables(null, null, drawable0, null);
            soldNo = 1;
            priceNo = 0;
        } else if (soldNo == 1) {
            map.put("sell_type", 2);
            map.put("page_index", 1);
            map.put("type", type);
            secondCategoryPresenter.categorySearch(map);
            sold.setCompoundDrawables(null, null, drawable1, null);
            soldNo = 2;
        } else {
            map.put("sell_type", 1);
            map.put("page_index", 1);
            map.put("type", type);
            secondCategoryPresenter.categorySearch(map);
            sold.setCompoundDrawables(null, null, drawable0, null);
            soldNo = 1;
        }
    }

    private void getCategory() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("action", "commodity_category_goods");
        map.put("category_id", category_id);
        map.put("page_index", 1);
        secondCategoryPresenter.categorySearch(map);
    }
}
